//
//  UUOrderModel.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 24/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderModel.h"

@implementation UUOrderDispatchItemModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addManualMappingDict:@{
                                     @"motorcadeId" : @"fleetId",
                                     @"motorcadeName" : @"fleetName",
                                     @"busName" : @"carNum",
                                     @"busId" : @"carId",
                                     @"carId" : @"busId",
                                     
                                     @"fleetId" : @"motorcadeId",
                                     @"fleetName" : @"motorcadeName"
                                     }];
    }
    return self;
}

@end

@implementation UUOrderModel

+ (void)initialize {
    if (self == [UUOrderModel class]) {
        [self setUserCalculateForCN:@"dispatch"];
    }
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addManualMappingDict:@{
                                     @"busNum" : @"carNum",
                                     @"vendorStatus" : @"status",
                                     @"busCategoryId" : @"carCategoryId",
                                     }];
    }
    return self;
}

+ (NSString *)getPrimaryKey {
    return @"id";
}

- (id)userGetValueForModel:(LKDBProperty *)property {
    if ([property.propertyName isEqualToString:@"dispatch"]) {
        if (self.dispatch.count > 0) {
            NSMutableString *dispatchString = [NSMutableString new];
            for (UUOrderDispatchItemModel *item in self.dispatch) {
                NSString *itemString = [NSString stringWithFormat:@"%@,%@,%@,",
                                                 item.fleetId,
                                                 item.carId,
                                                 item.driverId];
                [dispatchString appendFormat:@"%@_", itemString];
            }
            
            if (dispatchString.length > 0) {
                [dispatchString deleteCharactersInRange:NSMakeRange(dispatchString.length - 1, 1)];
            }
            
            return dispatchString;
        } else {
            return nil;
        }
    } else {
        return [super userGetValueForModel:property];
    }
}

- (void)userSetValueForModel:(LKDBProperty *)property value:(id)value {
    if ([property.propertyName isEqualToString:@"dispatch"]) {
        NSArray *itemStrings = [value componentsSeparatedByString:@"_"];
        if (itemStrings.count > 0) {
            NSMutableArray<UUOrderDispatchItemModel *> *dispatch = [NSMutableArray arrayWithCapacity:itemStrings.count];
            for (NSString *itemString in itemStrings) {
                NSArray *itemComponents = [itemString componentsSeparatedByString:@","];
                UUOrderDispatchItemModel *item = [UUOrderDispatchItemModel new];
                item.fleetId = itemComponents[0];
                item.carId = itemComponents[1];
                item.driverId = itemComponents[2];
                [dispatch addObject:item];
            }
            self.dispatch = dispatch;
        }
    } else {
        [super userSetValueForModel:property value:value];
    }
}

- (Class)classAtInsideOfObjectWithProperty:(NSString *)propertyName {
    if ([propertyName isEqualToString:@"dispatch"]) {
        return [UUOrderDispatchItemModel class];
    } else {
        return [super classAtInsideOfObjectWithProperty:propertyName];
    }
}

@end

@implementation UUOrderDetailModel

- (UUAddressModel *)departureAddressModel {
    return [UUAddressModel addressWithJSON:self.departure];
}

- (UUAddressModel *)arrivalAddressModel {
    return [UUAddressModel addressWithJSON:self.arrival];
}

@end
